import {Feed} from "../sql/domin";
import {arrayToMap, deleteSql, insertSql, objectToMap, selectSql, updateSql} from "../util/sqlUtils";

export class FeedMapper{
    public static InsertFeed(feed:Feed){
        return insertSql('bug_feeding',objectToMap(feed))
    }

    public static InsertFeedList(feed:Feed[]){
        return insertSql('bug_feeding',arrayToMap(feed))
    }

    public static selectFeedList(deptId:number,tableType: 'Feed' | 'Head' | 'Bzz'){
        return selectSql('bug_feeding',
                new Map<string,any>([
                    ['deptId',deptId],
                    ['tableType',tableType]
                ])
            )
    }

    public static selectFeedById(id:number,tableType:'Feed' | 'Head' | 'Bzz'){
        return selectSql('bug_feeding',
                new Map<string,any>([
                    ['id',id],
                    ['tableType',tableType]
                ])
            )
    }

    public static updateFeedById(feed:Feed,tableType: 'Feed' | 'Head' | 'Bzz'){
        return updateSql('bug_feeding',objectToMap(feed),
            `where id = ${feed.id} and tableType = '${tableType}'`)
    }

    public static updateFeedTableStatusById(id:number,tableStatus:string,tableType: 'Feed' | 'Head' | 'Bzz'){
        return updateSql('bug_feeding',new Map([['tableStatus',tableStatus]]),
            `where id = ${id} and tableType = '${tableType}'`)
    }

    public static deleteFeed(type:'Feed' | 'Head' | 'Bzz' = 'Feed'){
        return deleteSql('bug_feeding',
            `where date('now') != date(reloadDay) and tableType = '${type}'`)
    }

	public static deleteFeedRecord(){
	    return deleteSql('bug_feeding',
	        `where tableStatus = '0' and tableType = 'Feed' and updatedTime = null`)
	}

	public static deleteAudit(type:'Head' | 'Bzz'){
		return deleteSql('bug_feeding',
		    `where (tableStatus = '1' and tableType = '${type}') or date('now') != date(reloadDay)`)
	}

	public static deleteFeedById(id:number,type:'Feed' | 'Head' | 'Bzz' = 'Feed'){
	    return deleteSql('bug_feeding',
	        `where id = ${id} and tableType = '${type}'`)
	}

}
